﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace XML
{
    public class RoleRelationshipDal
    {
        #region  BasicMethod

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("product_id", "role_relationship");
        }

        public bool Exists(int product_id, int role_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from role_relationship");
            strSql.Append(" where product_id=@product_id and role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@role_id", SqlDbType.Int,4)			};
            parameters[0].Value = product_id;
            parameters[1].Value = role_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool Add(Model.role_relationship model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into role_relationship(");
            strSql.Append("product_id,role_id,linked_role,maxnumber,minnumber,cust_val_func)");
            strSql.Append(" values (");
            strSql.Append("@product_id,@role_id,@linked_role,@maxnumber,@minnumber,@cust_val_func)");
            SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@role_id", SqlDbType.Int,4),
					new SqlParameter("@linked_role", SqlDbType.VarChar,50),
					new SqlParameter("@maxnumber", SqlDbType.VarChar,10),
					new SqlParameter("@minnumber", SqlDbType.VarChar,10),
					new SqlParameter("@cust_val_func", SqlDbType.VarChar,50)};
            parameters[0].Value = model.product_id;
            parameters[1].Value = model.role_id;
            parameters[2].Value = model.linked_role;
            parameters[3].Value = model.maxnumber;
            parameters[4].Value = model.minnumber;
            parameters[5].Value = model.cust_val_func;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(Model.role_relationship model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update role_relationship set ");
            strSql.Append("linked_role=@linked_role,");
            strSql.Append("maxnumber=@maxnumber,");
            strSql.Append("minnumber=@minnumber,");
            strSql.Append("cust_val_func=@cust_val_func");
            strSql.Append(" where product_id=@product_id and role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@linked_role", SqlDbType.VarChar,50),
					new SqlParameter("@maxnumber", SqlDbType.VarChar,10),
					new SqlParameter("@minnumber", SqlDbType.VarChar,10),
					new SqlParameter("@cust_val_func", SqlDbType.VarChar,50),
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@role_id", SqlDbType.Int,4)};
            parameters[0].Value = model.linked_role;
            parameters[1].Value = model.maxnumber;
            parameters[2].Value = model.minnumber;
            parameters[3].Value = model.cust_val_func;
            parameters[4].Value = model.product_id;
            parameters[5].Value = model.role_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public bool Delete(int product_id, int role_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from role_relationship ");
            strSql.Append(" where product_id=@product_id and role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@role_id", SqlDbType.Int,4)			};
            parameters[0].Value = product_id;
            parameters[1].Value = role_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        public Model.role_relationship GetModel(int product_id, int role_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 product_id,role_id,linked_role,maxnumber,minnumber,cust_val_func from role_relationship ");
            strSql.Append(" where product_id=@product_id and role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@role_id", SqlDbType.Int,4)			};
            parameters[0].Value = product_id;
            parameters[1].Value = role_id;

            Model.role_relationship model = new Model.role_relationship();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public Model.role_relationship DataRowToModel(DataRow row)
        {
            Model.role_relationship model = new Model.role_relationship();
            if (row != null)
            {
                if (row["product_id"] != null && row["product_id"].ToString() != "")
                {
                    model.product_id = int.Parse(row["product_id"].ToString());
                }
                if (row["role_id"] != null && row["role_id"].ToString() != "")
                {
                    model.role_id = int.Parse(row["role_id"].ToString());
                }
                if (row["linked_role"] != null)
                {
                    model.linked_role = row["linked_role"].ToString();
                }
                if (row["maxnumber"] != null)
                {
                    model.maxnumber = row["maxnumber"].ToString();
                }
                if (row["minnumber"] != null)
                {
                    model.minnumber = row["minnumber"].ToString();
                }
                if (row["cust_val_func"] != null)
                {
                    model.cust_val_func = row["cust_val_func"].ToString();
                }
            }
            return model;
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select product_id,role_id,linked_role,maxnumber,minnumber,cust_val_func ");
            strSql.Append(" FROM role_relationship ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" product_id,role_id,linked_role,maxnumber,minnumber,cust_val_func ");
            strSql.Append(" FROM role_relationship ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM role_relationship ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.role_id desc");
            }
            strSql.Append(")AS Row, T.*  from role_relationship T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public List<Model.role_relationship> DataTableToList(DataTable dt)
        {
            List<Model.role_relationship> modelList = new List<Model.role_relationship>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.role_relationship model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        #endregion  BasicMethod

    }
}

